﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace E1_eMart
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        // For generating random numbers.
        private Random random = new Random();
        protected void Page_Load(object sender, EventArgs e)
        {
            Label2.Text = "";
            if (!this.IsPostBack)
            {
                // Create a random code and store it in the Session object.
                this.Session["CaptchaImageText"] = GenerateRandomCode();
            }
        }

        //
        // Returns a string of six random digits.
        //
        private string GenerateRandomCode()
        {
            string s = "";
            for (int i = 0; i < 6; i++)
                s = String.Concat(s, this.random.Next(10).ToString());
            return s;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!CbAgree.Checked)
            {
                Label2.Text = "Please check I agree";
                return;
            }
            if (this.CodeNumberTextBox.Text != this.Session["CaptchaImageText"].ToString())
            {
                this.CodeNumberTextBox.Text = "";
                this.Session["CaptchaImageText"] = GenerateRandomCode();
                this.RequiredFieldValidator7.ErrorMessage = "Captcha is not fit.";
                this.RequiredFieldValidator7.BackColor = System.Drawing.Color.Red;
                return;
            }

            DataClasses1DataContext db = new DataClasses1DataContext();
            try
            {
                TaiKhoan taiKhaoMoi = new TaiKhoan
                {
                    TenTaiKhoan = txtUsername.Text,
                    MatKhau = txtPassword.Text,
                    MaLoaiTaiKhoan = 1,
                    TrangThai = "Unlock",
                    DiemTinCayMua = 1000f,
                    DiemTinCayBan = 1000f
                };

                db.TaiKhoans.InsertOnSubmit(taiKhaoMoi);
                db.SubmitChanges();
                ThongTinTaiKhoan thongTinTaiKhoanMoi = new ThongTinTaiKhoan
                {
                    HoTen = txtFirstName.Text + " " + txtLastName.Text,
                    NgaySinh = DateTime.Parse(txtDate.Text + "/" + txtMonth.Text + "/" + txtYear.Text),
                    DiaChi = txtAddress.Text,
                    Email = txtEmail.Text,
                    NoiSinh = txtBirthplace.Text,
                    MaTheTinhDung = txtCreditcard.Text,
                    GioiTinh = cbbGender.SelectedItem.Value,
                    SoDienThoai = txtPhone.Text,
                    MaTaiKhoan = taiKhaoMoi.MaTaiKhoan
                };
                db.ThongTinTaiKhoans.InsertOnSubmit(thongTinTaiKhoanMoi);
                db.SubmitChanges();
                Label2.Text = "Successful registration !";
                Response.Redirect("index.aspx");
            }
            catch (Exception ex)
            {
                //Response.Write(ex);
            }
        }
    }
}